<?php

namespace App\Trip\Plane\Api;

use App\Trip\Plane\Interfaces\Result\Abstracts\AbstractFlightQueryResult;
use App\Trip\Plane\Interfaces\Result\Wrappers\PlaneQueryResultFilterWrapper;
use App\Trip\Plane\Interfaces\Result\Wrappers\PlaneQueryResultPaginatorWrapper;
use App\Trip\Plane\Interfaces\Result\Wrappers\PlaneQueryResultSortWrapper;
use App\Trip\Plane\Requests\TicketQueryRequest;

/**
 * 优先使用qunar查询 其他的供应商查询异步查询
 * 合并查询的结果的最低价
 */
class FlightQuery
{
    private PlaneQueryResultPaginatorWrapper $queryRes;
    private TicketQueryRequest               $request;

    public function __construct(AbstractFlightQueryResult $queryRes, TicketQueryRequest $ticket_request)
    {
        $queryRes       = new PlaneQueryResultFilterWrapper($queryRes, $ticket_request);
        $queryRes       = new PlaneQueryResultSortWrapper($queryRes, $ticket_request);
        $queryRes       = new PlaneQueryResultPaginatorWrapper($queryRes, $ticket_request);
        $this->queryRes = $queryRes;
        $this->request  = $ticket_request;
    }

    public function getResult(): array
    {
        return [
            'list'           => $this->queryRes->getLists(),
            'total'          => $this->queryRes->getCount(),
            'page'           => $this->queryRes->getPage(),
            'last_page'      => $this->queryRes->getLastPage(),
            'cities'         => $this->queryRes->getCities(),
            'filter_sources' => [
                'only_direct'     => $this->request->getOnlyDirect(),
                'depart_times'    => $this->queryRes->getDepartTimes(),
                'companies'       => $this->queryRes->getCompanies(),
                'depart_airports' => $this->queryRes->getDepartAirports(),
                'arrive_airports' => $this->queryRes->getArriveAirports(),
                'plane_types'     => $this->queryRes->getPlaneTypes(),
            ],
        ];
    }
}